home *** CD-ROM | disk | FTP | other *** search
/ Sky at Night 2007 June / SAN CD 6-2007 CD-ROM 25.iso / pc / Software / AstroGrav_Win / Java / jre1.6.0 / lib / rt.jar / sun / print / PSStreamPrintJob.class (.txt) < prev    next >
Encoding:
Java Class File  |  2006-11-29  |  10.4 KB  |  475 lines

  1. package sun.print;
  2.  
  3. import java.awt.print.PageFormat;
  4. import java.awt.print.Pageable;
  5. import java.awt.print.Paper;
  6. import java.awt.print.Printable;
  7. import java.awt.print.PrinterException;
  8. import java.awt.print.PrinterJob;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.Reader;
  12. import java.util.Locale;
  13. import java.util.Vector;
  14. import javax.print.CancelablePrintJob;
  15. import javax.print.Doc;
  16. import javax.print.DocFlavor;
  17. import javax.print.PrintException;
  18. import javax.print.PrintService;
  19. import javax.print.DocFlavor.BYTE_ARRAY;
  20. import javax.print.DocFlavor.INPUT_STREAM;
  21. import javax.print.DocFlavor.URL;
  22. import javax.print.attribute.Attribute;
  23. import javax.print.attribute.AttributeSet;
  24. import javax.print.attribute.AttributeSetUtilities;
  25. import javax.print.attribute.DocAttributeSet;
  26. import javax.print.attribute.HashPrintJobAttributeSet;
  27. import javax.print.attribute.HashPrintRequestAttributeSet;
  28. import javax.print.attribute.PrintJobAttribute;
  29. import javax.print.attribute.PrintJobAttributeSet;
  30. import javax.print.attribute.PrintRequestAttribute;
  31. import javax.print.attribute.PrintRequestAttributeSet;
  32. import javax.print.attribute.standard.Copies;
  33. import javax.print.attribute.standard.DocumentName;
  34. import javax.print.attribute.standard.Fidelity;
  35. import javax.print.attribute.standard.JobName;
  36. import javax.print.attribute.standard.JobOriginatingUserName;
  37. import javax.print.attribute.standard.Media;
  38. import javax.print.attribute.standard.MediaSize;
  39. import javax.print.attribute.standard.MediaSizeName;
  40. import javax.print.attribute.standard.OrientationRequested;
  41. import javax.print.attribute.standard.RequestingUserName;
  42. import javax.print.attribute.standard.MediaSize.NA;
  43. import javax.print.event.PrintJobAttributeListener;
  44. import javax.print.event.PrintJobEvent;
  45. import javax.print.event.PrintJobListener;
  46.  
  47. public class PSStreamPrintJob implements CancelablePrintJob {
  48.    private transient Vector jobListeners;
  49.    private transient Vector attrListeners;
  50.    private transient Vector listenedAttributeSets;
  51.    private PSStreamPrintService service;
  52.    private boolean fidelity;
  53.    private boolean printing = false;
  54.    private boolean printReturned = false;
  55.    private PrintRequestAttributeSet reqAttrSet = null;
  56.    private PrintJobAttributeSet jobAttrSet = null;
  57.    private PrinterJob job;
  58.    private Doc doc;
  59.    private InputStream instream = null;
  60.    private Reader reader = null;
  61.    private String jobName = "Java Printing";
  62.    private int copies = 1;
  63.    private MediaSize mediaSize;
  64.    private OrientationRequested orient;
  65.  
  66.    PSStreamPrintJob(PSStreamPrintService var1) {
  67.       this.mediaSize = NA.LETTER;
  68.       this.orient = OrientationRequested.PORTRAIT;
  69.       this.service = var1;
  70.    }
  71.  
  72.    public PrintService getPrintService() {
  73.       return this.service;
  74.    }
  75.  
  76.    public PrintJobAttributeSet getAttributes() {
  77.       synchronized(this) {
  78.          if (this.jobAttrSet == null) {
  79.             HashPrintJobAttributeSet var2 = new HashPrintJobAttributeSet();
  80.             return AttributeSetUtilities.unmodifiableView(var2);
  81.          } else {
  82.             return this.jobAttrSet;
  83.          }
  84.       }
  85.    }
  86.  
  87.    public void addPrintJobListener(PrintJobListener var1) {
  88.       synchronized(this) {
  89.          if (var1 != null) {
  90.             if (this.jobListeners == null) {
  91.                this.jobListeners = new Vector();
  92.             }
  93.  
  94.             this.jobListeners.add(var1);
  95.          }
  96.       }
  97.    }
  98.  
  99.    public void removePrintJobListener(PrintJobListener var1) {
  100.       synchronized(this) {
  101.          if (var1 != null && this.jobListeners != null) {
  102.             this.jobListeners.remove(var1);
  103.             if (this.jobListeners.isEmpty()) {
  104.                this.jobListeners = null;
  105.             }
  106.  
  107.          }
  108.       }
  109.    }
  110.  
  111.    private void closeDataStreams() {
  112.       if (this.doc != null) {
  113.          Object var1 = null;
  114.  
  115.          try {
  116.             var1 = this.doc.getPrintData();
  117.          } catch (IOException var25) {
  118.             return;
  119.          }
  120.  
  121.          if (this.instream != null) {
  122.             try {
  123.                this.instream.close();
  124.             } catch (IOException var23) {
  125.             } finally {
  126.                this.instream = null;
  127.             }
  128.          } else if (this.reader != null) {
  129.             try {
  130.                this.reader.close();
  131.             } catch (IOException var21) {
  132.             } finally {
  133.                this.reader = null;
  134.             }
  135.          } else if (var1 instanceof InputStream) {
  136.             try {
  137.                ((InputStream)var1).close();
  138.             } catch (IOException var20) {
  139.             }
  140.          } else if (var1 instanceof Reader) {
  141.             try {
  142.                ((Reader)var1).close();
  143.             } catch (IOException var19) {
  144.             }
  145.          }
  146.  
  147.       }
  148.    }
  149.  
  150.    private void notifyEvent(int var1) {
  151.       synchronized(this) {
  152.          if (this.jobListeners != null) {
  153.             PrintJobEvent var4 = new PrintJobEvent(this, var1);
  154.  
  155.             for(int var5 = 0; var5 < this.jobListeners.size(); ++var5) {
  156.                PrintJobListener var3 = (PrintJobListener)this.jobListeners.elementAt(var5);
  157.                switch (var1) {
  158.                   case 101:
  159.                      var3.printJobCanceled(var4);
  160.                      break;
  161.                   case 102:
  162.                      var3.printJobCompleted(var4);
  163.                      break;
  164.                   case 103:
  165.                      var3.printJobFailed(var4);
  166.                   case 104:
  167.                   default:
  168.                      break;
  169.                   case 105:
  170.                      var3.printJobNoMoreEvents(var4);
  171.                      break;
  172.                   case 106:
  173.                      var3.printDataTransferCompleted(var4);
  174.                }
  175.             }
  176.          }
  177.  
  178.       }
  179.    }
  180.  
  181.    public void addPrintJobAttributeListener(PrintJobAttributeListener var1, PrintJobAttributeSet var2) {
  182.       synchronized(this) {
  183.          if (var1 != null) {
  184.             if (this.attrListeners == null) {
  185.                this.attrListeners = new Vector();
  186.                this.listenedAttributeSets = new Vector();
  187.             }
  188.  
  189.             this.attrListeners.add(var1);
  190.             if (var2 == null) {
  191.                var2 = new HashPrintJobAttributeSet();
  192.             }
  193.  
  194.             this.listenedAttributeSets.add(var2);
  195.          }
  196.       }
  197.    }
  198.  
  199.    public void removePrintJobAttributeListener(PrintJobAttributeListener var1) {
  200.       synchronized(this) {
  201.          if (var1 != null && this.attrListeners != null) {
  202.             int var3 = this.attrListeners.indexOf(var1);
  203.             if (var3 != -1) {
  204.                this.attrListeners.remove(var3);
  205.                this.listenedAttributeSets.remove(var3);
  206.                if (this.attrListeners.isEmpty()) {
  207.                   this.attrListeners = null;
  208.                   this.listenedAttributeSets = null;
  209.                }
  210.  
  211.             }
  212.          }
  213.       }
  214.    }
  215.  
  216.    public void print(Doc var1, PrintRequestAttributeSet var2) throws PrintException {
  217.       synchronized(this) {
  218.          if (this.printing) {
  219.             throw new PrintException("already printing");
  220.          }
  221.  
  222.          this.printing = true;
  223.       }
  224.  
  225.       this.doc = var1;
  226.       DocFlavor var3 = var1.getDocFlavor();
  227.  
  228.       Object var4;
  229.       try {
  230.          var4 = var1.getPrintData();
  231.       } catch (IOException var14) {
  232.          this.notifyEvent(103);
  233.          throw new PrintException("can't get print data: " + var14.toString());
  234.       }
  235.  
  236.       if (var3 != null && this.service.isDocFlavorSupported(var3)) {
  237.          this.initializeAttributeSets(var1, var2);
  238.          this.getAttributeValues(var3);
  239.          String var5 = var3.getRepresentationClassName();
  240.          if (!var3.equals(INPUT_STREAM.GIF) && !var3.equals(INPUT_STREAM.JPEG) && !var3.equals(INPUT_STREAM.PNG) && !var3.equals(BYTE_ARRAY.GIF) && !var3.equals(BYTE_ARRAY.JPEG) && !var3.equals(BYTE_ARRAY.PNG)) {
  241.             if (!var3.equals(URL.GIF) && !var3.equals(URL.JPEG) && !var3.equals(URL.PNG)) {
  242.                if (var5.equals("java.awt.print.Pageable")) {
  243.                   try {
  244.                      this.pageableJob((Pageable)var1.getPrintData(), this.reqAttrSet);
  245.                   } catch (ClassCastException var7) {
  246.                      this.notifyEvent(103);
  247.                      throw new PrintException(var7);
  248.                   } catch (IOException var8) {
  249.                      this.notifyEvent(103);
  250.                      throw new PrintException(var8);
  251.                   }
  252.                } else if (var5.equals("java.awt.print.Printable")) {
  253.                   try {
  254.                      this.printableJob((Printable)var1.getPrintData(), this.reqAttrSet);
  255.                   } catch (ClassCastException var9) {
  256.                      this.notifyEvent(103);
  257.                      throw new PrintException(var9);
  258.                   } catch (IOException var10) {
  259.                      this.notifyEvent(103);
  260.                      throw new PrintException(var10);
  261.                   }
  262.                } else {
  263.                   this.notifyEvent(103);
  264.                   throw new PrintException("unrecognized class: " + var5);
  265.                }
  266.             } else {
  267.                try {
  268.                   this.printableJob(new ImagePrinter((java.net.URL)var4), this.reqAttrSet);
  269.                } catch (ClassCastException var11) {
  270.                   this.notifyEvent(103);
  271.                   throw new PrintException(var11);
  272.                }
  273.             }
  274.          } else {
  275.             try {
  276.                this.instream = var1.getStreamForBytes();
  277.                this.printableJob(new ImagePrinter(this.instream), this.reqAttrSet);
  278.             } catch (ClassCastException var12) {
  279.                this.notifyEvent(103);
  280.                throw new PrintException(var12);
  281.             } catch (IOException var13) {
  282.                this.notifyEvent(103);
  283.                throw new PrintException(var13);
  284.             }
  285.          }
  286.       } else {
  287.          this.notifyEvent(103);
  288.          throw new PrintJobFlavorException("invalid flavor", var3);
  289.       }
  290.    }
  291.  
  292.    public void printableJob(Printable var1, PrintRequestAttributeSet var2) throws PrintException {
  293.       try {
  294.          synchronized(this) {
  295.             if (this.job != null) {
  296.                throw new PrintException("already printing");
  297.             }
  298.  
  299.             this.job = new PSPrinterJob();
  300.          }
  301.  
  302.          this.job.setPrintService(this.getPrintService());
  303.          PageFormat var3 = new PageFormat();
  304.          if (this.mediaSize != null) {
  305.             Paper var4 = new Paper();
  306.             var4.setSize((double)this.mediaSize.getX(25400) * (double)72.0F, (double)this.mediaSize.getY(25400) * (double)72.0F);
  307.             var4.setImageableArea((double)72.0F, (double)72.0F, var4.getWidth() - (double)144.0F, var4.getHeight() - (double)144.0F);
  308.             var3.setPaper(var4);
  309.          }
  310.  
  311.          if (this.orient == OrientationRequested.REVERSE_LANDSCAPE) {
  312.             var3.setOrientation(2);
  313.          } else if (this.orient == OrientationRequested.LANDSCAPE) {
  314.             var3.setOrientation(0);
  315.          }
  316.  
  317.          this.job.setPrintable(var1, var3);
  318.          this.job.print(var2);
  319.          this.notifyEvent(102);
  320.       } catch (PrinterException var10) {
  321.          this.notifyEvent(103);
  322.          throw new PrintException(var10);
  323.       } finally {
  324.          this.printReturned = true;
  325.       }
  326.  
  327.    }
  328.  
  329.    public void pageableJob(Pageable var1, PrintRequestAttributeSet var2) throws PrintException {
  330.       try {
  331.          synchronized(this) {
  332.             if (this.job != null) {
  333.                throw new PrintException("already printing");
  334.             }
  335.  
  336.             this.job = new PSPrinterJob();
  337.          }
  338.  
  339.          this.job.setPrintService(this.getPrintService());
  340.          this.job.setPageable(var1);
  341.          this.job.print(var2);
  342.          this.notifyEvent(102);
  343.       } catch (PrinterException var10) {
  344.          this.notifyEvent(103);
  345.          throw new PrintException(var10);
  346.       } finally {
  347.          this.printReturned = true;
  348.       }
  349.  
  350.    }
  351.  
  352.    private synchronized void initializeAttributeSets(Doc var1, PrintRequestAttributeSet var2) {
  353.       this.reqAttrSet = new HashPrintRequestAttributeSet();
  354.       this.jobAttrSet = new HashPrintJobAttributeSet();
  355.       if (var2 != null) {
  356.          this.reqAttrSet.addAll(var2);
  357.          Attribute[] var3 = var2.toArray();
  358.  
  359.          for(int var4 = 0; var4 < var3.length; ++var4) {
  360.             if (var3[var4] instanceof PrintJobAttribute) {
  361.                this.jobAttrSet.add(var3[var4]);
  362.             }
  363.          }
  364.       }
  365.  
  366.       DocAttributeSet var12 = var1.getAttributes();
  367.       if (var12 != null) {
  368.          Attribute[] var11 = var12.toArray();
  369.  
  370.          for(int var5 = 0; var5 < var11.length; ++var5) {
  371.             if (var11[var5] instanceof PrintRequestAttribute) {
  372.                this.reqAttrSet.add(var11[var5]);
  373.             }
  374.  
  375.             if (var11[var5] instanceof PrintJobAttribute) {
  376.                this.jobAttrSet.add(var11[var5]);
  377.             }
  378.          }
  379.       }
  380.  
  381.       String var13 = "";
  382.  
  383.       try {
  384.          var13 = System.getProperty("user.name");
  385.       } catch (SecurityException var10) {
  386.       }
  387.  
  388.       if (var13 != null && !var13.equals("")) {
  389.          this.jobAttrSet.add(new JobOriginatingUserName(var13, (Locale)null));
  390.       } else {
  391.          RequestingUserName var6 = (RequestingUserName)var2.get(RequestingUserName.class);
  392.          if (var6 != null) {
  393.             this.jobAttrSet.add(new JobOriginatingUserName(var6.getValue(), var6.getLocale()));
  394.          } else {
  395.             this.jobAttrSet.add(new JobOriginatingUserName("", (Locale)null));
  396.          }
  397.       }
  398.  
  399.       if (this.jobAttrSet.get(JobName.class) == null) {
  400.          if (var12 != null && var12.get(DocumentName.class) != null) {
  401.             DocumentName var16 = (DocumentName)var12.get(DocumentName.class);
  402.             JobName var15 = new JobName(var16.getValue(), var16.getLocale());
  403.             this.jobAttrSet.add(var15);
  404.          } else {
  405.             String var7 = "JPS Job:" + var1;
  406.  
  407.             try {
  408.                Object var8 = var1.getPrintData();
  409.                if (var8 instanceof java.net.URL) {
  410.                   var7 = ((java.net.URL)((java.net.URL)var1.getPrintData())).toString();
  411.                }
  412.             } catch (IOException var9) {
  413.             }
  414.  
  415.             JobName var14 = new JobName(var7, (Locale)null);
  416.             this.jobAttrSet.add(var14);
  417.          }
  418.       }
  419.  
  420.       this.jobAttrSet = AttributeSetUtilities.unmodifiableView(this.jobAttrSet);
  421.    }
  422.  
  423.    private void getAttributeValues(DocFlavor var1) throws PrintException {
  424.       if (this.reqAttrSet.get(Fidelity.class) == Fidelity.FIDELITY_TRUE) {
  425.          this.fidelity = true;
  426.       } else {
  427.          this.fidelity = false;
  428.       }
  429.  
  430.       Attribute[] var4 = this.reqAttrSet.toArray();
  431.  
  432.       for(int var5 = 0; var5 < var4.length; ++var5) {
  433.          Attribute var2 = var4[var5];
  434.          Class var3 = var2.getCategory();
  435.          if (this.fidelity) {
  436.             if (!this.service.isAttributeCategorySupported(var3)) {
  437.                this.notifyEvent(103);
  438.                throw new PrintJobAttributeException("unsupported category: " + var3, var3, (Attribute)null);
  439.             }
  440.  
  441.             if (!this.service.isAttributeValueSupported(var2, var1, (AttributeSet)null)) {
  442.                this.notifyEvent(103);
  443.                throw new PrintJobAttributeException("unsupported attribute: " + var2, (Class)null, var2);
  444.             }
  445.          }
  446.  
  447.          if (var3 == JobName.class) {
  448.             this.jobName = ((JobName)var2).getValue();
  449.          } else if (var3 == Copies.class) {
  450.             this.copies = ((Copies)var2).getValue();
  451.          } else if (var3 == Media.class) {
  452.             if (var2 instanceof MediaSizeName && this.service.isAttributeValueSupported(var2, (DocFlavor)null, (AttributeSet)null)) {
  453.                this.mediaSize = MediaSize.getMediaSizeForName((MediaSizeName)var2);
  454.             }
  455.          } else if (var3 == OrientationRequested.class) {
  456.             this.orient = (OrientationRequested)var2;
  457.          }
  458.       }
  459.  
  460.    }
  461.  
  462.    public void cancel() throws PrintException {
  463.       synchronized(this) {
  464.          if (!this.printing) {
  465.             throw new PrintException("Job is not yet submitted.");
  466.          } else if (this.job != null && !this.printReturned) {
  467.             this.job.cancel();
  468.             this.notifyEvent(101);
  469.          } else {
  470.             throw new PrintException("Job could not be cancelled.");
  471.          }
  472.       }
  473.    }
  474. }
  475.